package com.whmall.service.crm.provider;

import com.whmall.component.entity.dto.OperatorReqDTO;
import com.whmall.component.entity.dto.PageReqDTO;
import com.whmall.component.entity.dto.PageRspDTO;
import com.whmall.service.crm.api.CompanyCertificateChemicalApi;
import com.whmall.service.crm.entity.dto.companycertificatechemical.*;
import com.whmall.service.crm.module.companycertificatechemical.CompanyCertificateChemicalApproveQueryCriteria;
import com.whmall.service.crm.module.companycertificatechemical.CompanyCertificateChemicalService;
import com.whmall.service.crm.module.companycertificatechemical.CompanyCertificateChemicalViewBuilder;
import com.whmall.service.crm.module.companycertificatechemical.entity.qo.CompanyCertificateChemicalQO;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Method;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * CompanyCertificateChemicalProvider
 *
 * @author alex
 * @since 2020/8/17
 */
@DubboService(interfaceClass = CompanyCertificateChemicalApi.class, methods = {
        @Method(name = "ineffectiveByChemicalAndCompanyId", retries = 0, timeout = 60 * 1000),
        @Method(name = "rebuildAllApproveFromAssociated", retries = 0, timeout = 30 * 1000)
})
@RequiredArgsConstructor
public class CompanyCertificateChemicalProvider implements CompanyCertificateChemicalApi {

    private final CompanyCertificateChemicalService service;
    private final CompanyCertificateChemicalViewBuilder viewBuilder;

    @Override
    public List<AssociateChemicalResultRspDTO> associateChemicals(OperatorReqDTO operator, Long companyId, Long certificateId, Long bindToUserId,
                                                                  List<AssociateChemicalReqDTO> associateRequests,
                                                                  List<AssociateChemicalDetectResultReqDTO> associateChemicalDetectResults) {
        return service.associateChemicals(operator, companyId, certificateId, bindToUserId, associateRequests, associateChemicalDetectResults);
    }

    @Override
    @Transactional
    public CompanyCertificateChemicalRspDTO removeChemical(OperatorReqDTO operator, Long companyId, Long certificateId, Long associatedId) {
        return service.removeChemical(operator, companyId, certificateId, associatedId);
    }

    @Override
    public void deleteAllApproveList() {
        service.deleteAllApproveList();
    }

    @Override
    public void rebuildAllApproveFromAssociated() {
        service.rebuildAllApproveFromAssociated();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<CompanyCertificateChemicalRspDTO> approval(Long requesterId, Long companyId, Long certificateId, ApprovalCertificateChemicalReqDTO request) {
        return service.approval(requesterId, companyId, certificateId, request);
    }

    @Override
    public PageRspDTO<CompanyCertificateChemicalPageItemRspDTO> page(PageReqDTO pageRequest, Long companyId, Long certificateId) {
        return viewBuilder.buildAssociatePage(pageRequest, CompanyCertificateChemicalQO.builder()
                .companyId(companyId).certificateId(certificateId).orderBy(" create_time DESC").build());
    }

    @Override
    public PageRspDTO<CompanyCertificateChemicalPageItemRspDTO> page(PageReqDTO pageRequest, CompanyCertificateChemicalQueryReqDTO queryReqDTO) {
        CompanyCertificateChemicalQO qo = new CompanyCertificateChemicalQO();
        BeanUtils.copyProperties(queryReqDTO, qo);
        qo.setOrderBy(" create_time DESC");
        return viewBuilder.buildAssociatePage(pageRequest, qo);
    }

    @Override
    public PageRspDTO<CompanyCertificateChemicalApprovePageItemRspDTO> pageForApprove(PageReqDTO pageRequest, CompanyCertificateChemicalApproveQueryCriteria criteria) {
        return viewBuilder.buildApprovePage(pageRequest, criteria);
    }

    @Override
    public int countBy(CompanyCertificateChemicalQueryReqDTO queryReqDTO) {
        return service.countBy(queryReqDTO);
    }

    @Override
    public List<CompanyCertificateChemicalStatisticDTO> statisticBy(CompanyCertificateChemicalQueryReqDTO queryReqDTO) {
        return service.statisticBy(queryReqDTO);
    }

    @Override
    public List<CompanyCertificateChemicalRspDTO> selectBy(CompanyCertificateChemicalQueryReqDTO queryReqDTO) {
        return service.selectBy(queryReqDTO);
    }

    @Override
    public Integer certificate(CompanyCertificateChemicalOptionDTO option) {
        return service.certificate(option);
    }

    @Override
    public String certificates(List<CompanyCertificateChemicalOptionDTO> options) {
        return service.certificates(options);
    }

    @Override
    public List<CompanyCertificateChemicalPageItemRspDTO> selectByIds(List<Long> ids) {
        return service.selectByIds(ids);
    }

    @Override
    public void ineffectiveByChemicalAndCompanyId(long certificateId, List<Long> chemicalIds, long companyId) {
        service.ineffectiveByChemicalAndCompanyId(certificateId, chemicalIds, companyId);
    }
}
